﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;

namespace WsProtocol.IEC62056
{
    // hxh 2018-5 62056
    [Serializable]
    public class CosemObject 
    {
        private object m_tag = null;
        private string m_OBIS;        
        private string m_classid; //类编号
        private string m_AttrSeq;
        private string m_AmName;　//数据名称
        private ElementData m_DataNode; //数据节点
        private ElementData m_ParEleNode; //附加Attr列表，如读负荷曲线有附加参数
        private string m_result;　//解析结果   
      
        #region Public Properties
        public object Tag
        {
            get
            {
                return m_tag;
            }
            set
            {
                m_tag = value;
            }
        }
        public string OBIS
        {
            get
            {
                return m_OBIS;
            }
            set
            {
                m_OBIS = value;
            }
        }
      
        public string classid
        {
            get
            {
                return m_classid;
            }
            set
            {
                m_classid = value;
            }
        }
        public string AttrSeq
        {
            get
            {
                return m_AttrSeq;
            }
            set
            {
                m_AttrSeq = value;
            }
        }
        public string AmName
        {
            get
            {
                return m_AmName;
            }
            set
            {
                m_AmName = value;
            }
        }
        public ElementData DataNode
        {
            get
            {
                return m_DataNode;
            }
            set
            {
                m_DataNode = value;
            }
        }

        public ElementData ParEleNode
        {
            get
            {
                return m_ParEleNode;
            }
            set
            {
                m_ParEleNode = value;
            }
        }
        public string result
        {
            get
            {
                return m_result;
            }
            set
            {
                m_result = value;
            }
        }
        #endregion

        public CosemObject()
        {
            m_DataNode = new ElementData();
            //m_ParEleNode = new ElementData();

        }

        public CosemObject(object OIData,object eleNode, object tag)
        {
            m_tag = tag;

            if (m_DataNode == null)
                m_DataNode = new ElementData();
            else
                m_DataNode = (ElementData)eleNode;
          
            try
            {
                object[] obArray = (object[])OIData;
                m_OBIS = obArray[0].ToString();
                m_AmName = obArray[1].ToString();
                m_classid = obArray[2].ToString();
                m_AttrSeq = obArray[3].ToString();
                

            }
            catch (Exception ex) { }


        }

        public CosemObject DeepClone()
        {
            ////新建一个
            CosemObject newobj = new CosemObject();

            //复制属性
            PropertyInfo[] fromFields = typeof(CosemObject).GetProperties();
            SetProperties(fromFields, this, newobj);

            //复制包含的属性列表
            newobj.DataNode = this.DataNode.DeepClone();
           // newobj.ParEleNode = this.ParEleNode.DeepClone();

            //复制附加OI列表
            
            return newobj;
        }

        /// <summary>
        /// 复制类属性 3参数
        /// </summary>
        /// <param name="fromFields"></param>
        /// <param name="fromRecord"></param>
        /// <param name="toRecord"></param>
        private void SetProperties(PropertyInfo[] fromFields,
                                         object fromRecord,
                                         object toRecord)
        {
            PropertyInfo fromField = null;

            try
            {

                if (fromFields == null)
                {
                    return;
                }

                for (int f = 0; f < fromFields.Length; f++)
                {

                    fromField = (PropertyInfo)fromFields[f];

                    fromField.SetValue(toRecord,
                                       fromField.GetValue(fromRecord, null),
                                       null);
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        public int AddNode(ElementData mNode)
        {
            //if (m_eleNodeList == null)
            //    m_eleNodeList = new List<ElementData>();
            //m_eleNodeList.Add(mNode);
            m_DataNode = mNode;
            return 1;
        }

        public int AddAppendOb(ElementData mOb)
        {
            //if (m_PareleNode == null)
            //    m_PareleNode = new List<ElementData>();
            //m_PareleNode.Add(mOb);
            m_ParEleNode = mOb;
            return 1;
        }

       
    }


}
